package br.com.conceito.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.conveito.entidade.Licitacao;

public class LicitacaoDAO {

	public void salvar(Licitacao licitacao){
		StringBuilder sql = new StringBuilder();
		sql.append("insert into licitacao(nome,descricao,dt_inicio,dt_fim) values(?,?,?,?)");
		Connection connection;
		try {
			connection = FabricaDAO.getInstancia().getConection();
			PreparedStatement statement = connection.prepareStatement(sql.toString());
			statement.setString(1, licitacao.getNome());
			statement.setString(2, licitacao.getDescricao());
			statement.setDate(3, new Date( licitacao.getDtInicio().getTime()));
			statement.setDate(4, new Date( licitacao.getDtFim().getTime()));
			statement.execute();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<Licitacao> carregarTodos(){
		List<Licitacao> licitacaos = new ArrayList<Licitacao>();
		StringBuilder sql = new StringBuilder();
		sql.append("select id,nome,descricao,dt_inicio,dt_fim from licitacao");
		Connection connection;
		try {
			connection = FabricaDAO.getInstancia().getConection();
			PreparedStatement statement = connection.prepareStatement(sql.toString());
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				Licitacao licitacao = new Licitacao();
				licitacao.setId(rs.getInt("id"));
				licitacao.setNome(rs.getString("nome"));
				licitacao.setDescricao(rs.getString("descricao"));
				licitacao.setDtInicio(rs.getDate("dt_inicio"));
				licitacao.setDtFim(rs.getDate("dt_fim"));
				licitacaos.add(licitacao);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return licitacaos;
	}
	
}

